#include <Wt/WContainerWidget>
#include <Wt/WStackedWidget>
#include <Wt/WApplication>
#include <Wt/WEnvironment>
#include <Wt/WTable>
#include <Wt/WTabWidget>
#include <Wt/WMessageBox>
#include <Wt/WText>
#include <QString>

#include "LoginWidget.h"
#include "CommonWidget.h"

class MainStack : public WContainerWidget
 {
     WStackedWidget *MyStack;
     LoginWidget *Login;
     CommonWidget *MainWidget;
     WApplication *app1;


 public:
     MainStack(WContainerWidget *parent,WApplication *app):WContainerWidget(parent),app1(app){
      parent->decorationStyle().setBackgroundColor(WColor(255,191,128,128));
      MyStack=new WStackedWidget(this);;

      if (app1->instance()->environment().supportsCookies()==true)
      {
          std::string id=app1->instance()->environment().getCookie("userid").c_str();
         // WMessageBox::show("login", WString("User id= ") +id , Ok | Cancel);

          if (id.compare("0") !=0)
          {

              int idd=boost::lexical_cast<double>(id.c_str());

              MyStack->addWidget(new CommonWidget(idd,app1));
              MyStack->setCurrentIndex(0);
          }
          else
          {
              MyStack->addWidget(Login=new LoginWidget());
              MyStack->setCurrentIndex(0);
              Login->loginSuccessful.connect(this,&MainStack::play);

          }
          //app1->instance()->setCookie("userid","1");
      }
      else
      {
       MyStack->addWidget(Login=new LoginWidget());
       MyStack->setCurrentIndex(0);
       Login->loginSuccessful.connect(this,&MainStack::play);

      }
    }



    void play(int User_Id){
        delete Login;
        std::string strName="userid";
        std::string strValue=QString::number(User_Id).toStdString();
        app1->instance()->setCookie(strName,strValue,1000);
        MyStack->addWidget(new CommonWidget(User_Id,app1));
        MyStack->setCurrentIndex(0);
      }
 };
